<?php defined('SYSPATH') or die('No direct script access.');

class auth {

    public static $PASSWORD_MIN_LENGTH = 5;
    public static $FORM_ID = '_formId';
    
    public static $ORG_ROLE_REPRESENTATIVE = 'representative';
    public static $ORG_ROLE_ORGANIZER = 'organizer';

    /**
     *
     * @param Array $person
     * @param YoseminDao $dao
     */
    public static function suggestLogin($person, $dao) {
        if (! trim($person['first_name'] . $person['last_name'])) {
            return '';
        }
        // john.doe
        $login = string::toLowerCaseAsciiText(trim($person['first_name']) . '.' . trim($person['last_name']));
        if ($dao->existsAccountWithLogin($login)) {
            if ($person['email']) {
                // email login
                $login = preg_replace('/(.*)@.*/', '\1', $person['email']);
            }
            if ($dao->existsAccountWithLogin($login)) {
                // doe.john
                $login = string::toLowerCaseAsciiText($person['last_name'] . '.' . $person['first_name']);
                if ($dao->existsAccountWithLogin($login)) {
                    // johndoe
                    $login = string::toLowerCaseAsciiText($person['first_name'] . $person['last_name']);
                    if ($dao->existsAccountWithLogin($login)) {
                        // j.doe
                        $login = string::toLowerCaseAsciiText(substr($person['first_name'], 0, 1) . '.' . $person['last_name']);
                        if ($dao->existsAccountWithLogin($login)) {
                            // doe.j
                            $login = string::toLowerCaseAsciiText($person['last_name'] . '.' . substr($person['first_name'], 0, 1));
                            if ($dao->existsAccountWithLogin($login)) {
                                $login = '';
                            }
                        }
                    }
                }
            }
        }
        return $login;
    }
    
    public static function passwordHash($login, $password) {
    	return sha1($login . $password);
    }
}
